package com.enjoy.concurrent.ch1.base;

/**
 * 演示join方法的使用
 */
public class UseJoin {

    static class Goddess implements Runnable {

        private final Thread thread;

        public Goddess(Thread thread) {
            this.thread = thread;
        }

        @Override
        public void run() {
            System.out.println("Goddess开始排队打饭");
            try {
                if (thread != null) {
                    thread.join();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }
    }

    static class GoddessBoyfriend implements Runnable {

        public void run() {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("GoddessBoyfriend开始排队打饭");
            System.out.println(Thread.currentThread().getName() + " GoddessBoyfriend打饭完成");
        }
    }

    public static void main(String[] args) throws Exception {

        GoddessBoyfriend goddessBoyfriend = new GoddessBoyfriend();
        Thread gbf = new Thread(goddessBoyfriend);

        Goddess goddess = new Goddess(gbf);
        Thread g = new Thread(goddess);

        g.start();
        gbf.start();

        System.out.println("lison开始排队打饭");
        g.join();
        Thread.sleep(2000);//让主线程休眠2秒
        System.out.println(Thread.currentThread().getName() + " lison打饭完成");
    }

}
